package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.proCarryOut.AppropriationDto;
import com.dg.cuckoo.admin.dto.proCarryOut.ProCarryOutDto;
import com.dg.cuckoo.admin.repository.AppropriationRepository;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.admin.service.SuperiorService;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


@Service
public class SuperiorServiceimpl implements SuperiorService {

    @Autowired
    private AppropriationRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;

    @Override
    public void save(String projectId, AppropriationDto dto)  {
        powerfulSave(1,projectId,dto);
    }

    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
    }

    @Override
    public AppropriationDto openInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        return repository.findById(id).get();
    }

    @Override
    public void update(String id,String projectId, AppropriationDto dto) {
        powerfulUpdate(1,id,projectId,dto);
    }

    @Override
    public String page(Integer page, Integer pageSize, String projectId) {
        return powerfulPage(1,page,pageSize,projectId);
    }

    @Override
    public void powerfulSave(Integer type, String projectId, AppropriationDto dto) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        checkSave(dto);
        dto.setProjectId(projectId);
        dto.setType(dto.getAmountSource());
        repository.save(dto);
    }

    @Override
    public void powerfulUpdate(Integer type, String id, String projectId, AppropriationDto dto) {
        if(id==null){
            throw new ServiceException("请选择id主键");
        }
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        dto.setId(id);
        dto.setProjectId(projectId);
        dto.setType(type);
        repository.save(dto);
    }

    @Override
    public String powerfulPage(Integer type, Integer page, Integer pageSize, String projectId) {
        if(projectId==null){
            throw new ServiceException("请选择项目");
        }
        Integer one = page;
        page = (page - 1) * pageSize;
        List<AppropriationDto> dtos = repository.findByProjectIdAndType(projectId, type);
        List<AppropriationDto> info = repository.page(type, projectId, page, pageSize);
        PageModel model = new PageModel(one, pageSize, dtos.size(), info);
        String amount = amount(dtos);
        JSONObject json = new JSONObject();
        json.put("PageModel",model);    //分页数据
        json.put("amount",amount);      //总金额
        return json.toString();
    }





    public void checkSave(AppropriationDto dto){

    }

    public String amount(List<AppropriationDto> dtos){
        BigDecimal bigDecimal = new BigDecimal("0");
        for(AppropriationDto a:dtos){
            bigDecimal.add(new BigDecimal(a.getAmount()));
        }
        return bigDecimal.toString();
    }
}
